package com.zhss.eshop.schedule.stock;

import com.zhss.eshop.order.domain.dto.OrderItemDTO;
import com.zhss.eshop.schedule.domain.SaleDeliveryScheduleResult;
import com.zhss.eshop.schedule.domain.dto.ScheduleOrderPickingItemDTO;
import com.zhss.eshop.schedule.domain.dto.ScheduleOrderSendOutDetailDTO;
import com.zhss.eshop.schedule.domain.model.ScheduleGoodsAllocationStock;
import com.zhss.eshop.schedule.domain.model.ScheduleGoodsAllocationStockDetail;
import com.zhss.eshop.schedule.domain.model.ScheduleGoodsStock;
import com.zhss.eshop.schedule.mapper.ScheduleGoodsAllocationStockDetailMapper;
import com.zhss.eshop.schedule.mapper.ScheduleGoodsAllocationStockMapper;
import com.zhss.eshop.schedule.mapper.ScheduleGoodsStockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 提交订单库存更新组件
 * @author suyuan
 *
 */
@Component
@Scope("prototype")
@Transactional(rollbackFor = Exception.class)
public class SubmitOrderScheduleStockUpdater extends AbstractScheduleStockUpdater {

	/**
	 * 商品库存管理的DAO组件
	 */
	@Autowired
	private ScheduleGoodsStockMapper goodsStockDAO;
	/**
	 * 货位库存管理的DAO组件
	 */
	@Autowired
	private ScheduleGoodsAllocationStockMapper goodsAllocationStockDAO;
	/**
	 * 货位库存明细管理的DAO组件
	 */
	@Autowired
	private ScheduleGoodsAllocationStockDetailMapper stockDetailDAO;

	/**
	 * 销售出库调度结果
	 */
	private SaleDeliveryScheduleResult scheduleResult;

	/**
	 * 更新商品库存
	 */
	@Override
	protected void updateGoodsStock() throws Exception {
		OrderItemDTO orderItem = scheduleResult.getOrderItem();
		ScheduleGoodsStock goodsStock = goodsStockDAO.getBySkuId(orderItem.getGoodsSkuId());
		if(goodsStock == null) {
			goodsStock = new ScheduleGoodsStock();
			goodsStock.setGoodsSkuId(orderItem.getGoodsSkuId());
			goodsStock.setAvailableStockQuantity(0L);
			goodsStock.setLockedStockQuantity(0L);
			goodsStock.setOutputStockQuantity(0L);
			goodsStockDAO.insertSelective(goodsStock);
		}
		goodsStock.setAvailableStockQuantity(goodsStock.getAvailableStockQuantity()
				- orderItem.getPurchaseQuantity());
		goodsStock.setLockedStockQuantity(goodsStock.getLockedStockQuantity() +
				orderItem.getPurchaseQuantity());
		goodsStockDAO.updateByPrimaryKeySelective(goodsStock);
	}

	/**
	 * 更新货位库存
	 */
	@Override
	protected void updateGoodsAllocationStock() throws Exception {
		List<ScheduleOrderPickingItemDTO> pickingItems = scheduleResult.getPickingItems();
		for(ScheduleOrderPickingItemDTO pickingItem : pickingItems) {
			ScheduleGoodsAllocationStock goodsAllocationStock = goodsAllocationStockDAO.getBySkuId(
					pickingItem.getGoodsAllocationId(), pickingItem.getGoodsSkuId());
			if(goodsAllocationStock == null) {
				goodsAllocationStock = new ScheduleGoodsAllocationStock();
				goodsAllocationStock.setGoodsAllocationId(pickingItem.getGoodsAllocationId());
				goodsAllocationStock.setGoodsSkuId(pickingItem.getGoodsSkuId());
				goodsAllocationStock.setAvailableStockQuantity(0L);
				goodsAllocationStock.setLockedStockQuantity(0L);
				goodsAllocationStock.setOutputStockQuantity(0L);
				goodsAllocationStockDAO.insertSelective(goodsAllocationStock);
			}
			goodsAllocationStock.setAvailableStockQuantity(goodsAllocationStock.getAvailableStockQuantity()
					- pickingItem.getPickingCount());
			goodsAllocationStock.setLockedStockQuantity(goodsAllocationStock.getLockedStockQuantity()
					+ pickingItem.getPickingCount());
			goodsAllocationStockDAO.updateByPrimaryKeySelective(goodsAllocationStock);
		}
	}

	/**
	 * 更新货位库存明细
	 */
	@Override
	protected void updateGoodsAllocationStockDetail() throws Exception {
		List<ScheduleOrderSendOutDetailDTO> sendOutDetails = scheduleResult.getSendOutDetails();
		for(ScheduleOrderSendOutDetailDTO sendOutDetail : sendOutDetails) {
			ScheduleGoodsAllocationStockDetail stockDetail = stockDetailDAO.selectByPrimaryKey(
					sendOutDetail.getGoodsAllocationStockDetailId());
			//可用库存减掉，锁定库存增加
			stockDetail.setCurrentStockQuantity(stockDetail.getCurrentStockQuantity()
					- sendOutDetail.getSendOutCount());
			stockDetail.setLockedStockQuantity(stockDetail.getLockedStockQuantity()
					+ sendOutDetail.getSendOutCount());
			stockDetailDAO.updateByPrimaryKeySelective(stockDetail);
		}
	}

	/**
	 * 设置库存更新组件需要的参数
	 */
	@Override
	public void setParameter(Object parameter) {
		this.scheduleResult = (SaleDeliveryScheduleResult) parameter;
	}

}
